<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>产品输入</title>

    <link rel="stylesheet" href="/node_modules/layui/dist/css/layui.css" />
    <link rel="stylesheet" href="/node_modules/datatables.net-dt/css/jquery.dataTables.min.css" />

    <script type="text/javascript" src="/node_modules/layui/dist/layui.js"></script>
    <script type="text/javascript" src="/node_modules/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="/node_modules/datatables.net/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="/js/config.js"></script>
    <script type="text/javascript" src="/js/site.js"></script>
</head>
<body style="padding: 10px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">品名</label>
            <div class="layui-input-block">
              <input type="text" id="name" name="name" lay-verify="required" autocomplete="off" placeholder="产品品名" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">库存</label>
            <div class="layui-input-block">
              <input type="text" id="quantity_in_stock" name="quantity_in_stock" lay-verify="required" autocomplete="off" placeholder="库存" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">单价</label>
            <div class="layui-input-block">
              <input type="text" id="unit_price" name="unit_price" lay-verify="required" autocomplete="off" placeholder="单价" class="layui-input">
            </div>
        </div>

    </form>

    <div class="layui-input-block">
        <button class="layui-btn" id="cmdSubmit">提交</button>
    </div>

    <script>
        $(document).ready(() => {
            $("#cmdSubmit").on("click", () => {
                let formData = new FormData();

                let name = $("#name").val();
                formData.append("name", name);

                let quantity_in_stock = $("#quantity_in_stock").val();
                formData.append("quantity_in_stock", quantity_in_stock);

                let unit_price = $("#unit_price").val();
                formData.append("unit_price", unit_price);

                let myUrl = apiurl + "/api/products";

                $.ajax({
                    url: myUrl,
                    type: 'POST',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false
                }).done(function (res) {
                    let index = parent.layer.getFrameIndex(window.name)
                    parent.layer.close(index)
                    parent.layer.alert("输入成功!")
                }).fail(function (res) {
                    layer.alert("输入失败！");
                });
            });
        })
    </script>



</body>
</html>